package com.baymax.test.scene.demo

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newSceneFlexContainer
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import org.http4k.client.OkHttp
import org.http4k.core.Body
import org.http4k.core.ContentType
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.with
import org.http4k.format.ConfigurableGson
import org.http4k.format.Gson.json
import org.http4k.format.asConfigurable
import org.http4k.format.withStandardMappings
import org.http4k.lens.contentType
import org.http4k.server.Http4kServer
import org.http4k.server.KtorCIO
import org.http4k.server.asServer
import java.io.Closeable

class HttpScene : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer {
            val http = HttpApp().apply {
                lifecycle.addObserver(this)
            }
            addButton("startServer") {
                val s = http.server.start()
                Log.d(TAG, "onCreateView: server $s")

            }

            addButton("stop") {
                http.server.stop()
            }

            addButton("request") {
                launch {
                    val inMemoryResponse: Response = http.request("hello")
                    println(inMemoryResponse.toMessage())
                }
            }
        }
    }


    class HttpApp : Closeable, DefaultLifecycleObserver {
        val app: HttpHandler = { request: Request ->
            println("start")
            Response(OK).with(
                JSON.obj(
                    "uri" to JSON.string(request.uri.toString()),
                    "params" to JSON.string(request.bodyString()),
                )
            )
        }
        val server: Http4kServer by lazy {
            //io.ktor.development
            //关闭hot reload
            System.setProperty("io.ktor.development", "false")

            app.asServer(KtorCIO(8000))
        }

        val client = app

        suspend fun request(route: String): Response {
            return withContext(Dispatchers.IO) {
                println("request in")
                client(Request(Method.GET, "http://127.0.0.1:8000/$route"))
            }
        }

        override fun close() {
            server.stop()
        }

        override fun onDestroy(owner: LifecycleOwner) {
            super.onDestroy(owner)
            close()
        }

    }

    companion object JSON : ConfigurableGson(
        GsonBuilder().serializeNulls().asConfigurable() /*            .withStandardMappings() */
            .done()
    )

}